package cn.edu.cdut.contentproviderdemo2;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.eliza.ascl.conpv.R;

public class MainActivity extends AppCompatActivity {

    TextView mContactTV;
    EditText username;
    EditText phone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContactTV = findViewById(R.id.ContactTV);
        username = findViewById(R.id.text1);
        phone = findViewById(R.id.text2);


    }

    public void queryClick(View view){
        mContactTV.setText("");
        String id = username.getText().toString();
        Uri uri = Uri.parse("content://cn.edu.cdut.provider/user/"+id);
        Cursor cursor = getContentResolver().query(uri,null,null,null,null);
        printQueryResult(cursor);
    }



    public void insertClick(View view){
        String name = username.getText().toString();
        String phoneNum = phone.getText().toString();

        Uri uri = Uri.parse("content://cn.edu.cdut.provider/user");

        ContentValues values = new ContentValues();
        values.put("username",name);
        values.put("phonenumber",phoneNum);

        getContentResolver().insert(uri,values);

        Toast.makeText(this,"已插入",Toast.LENGTH_SHORT).show();



    }

    public void deleteClick(View view){
        String name = username.getText().toString();

        Uri uri = Uri.parse("content://cn.edu.cdut.provider/user");
        getContentResolver().delete(uri,"username=?",new String[]{name});
        Toast.makeText(this,"已删除",Toast.LENGTH_SHORT).show();

    }

    public void updateClick(View view){
        String name = username.getText().toString();
        String phoneNum = phone.getText().toString();

        Uri uri = Uri.parse("content://cn.edu.cdut.provider/user");
        ContentValues values = new ContentValues();
        values.put("phonenumber",phoneNum);
        getContentResolver().update(uri,values,"username=?",new String[]{name});
        Toast.makeText(this,"已更新",Toast.LENGTH_SHORT).show();

    }

    public void queryAll(View view){
        mContactTV.setText("");
        Uri uri = Uri.parse("content://cn.edu.cdut.provider/user");

        Cursor cursor = getContentResolver().query(uri,null,null,null,null);
        printQueryResult(cursor);
    }

    private void printQueryResult(Cursor cursor){
        if(cursor != null){
            mContactTV.setText("");
            while (cursor.moveToNext()){
                String ID = cursor.getString(0);
                String contactName = cursor.getString(1);
                String phoneNumber = cursor.getString(2);
                mContactTV.append("联系人ID："+ID+"\n联系人姓名："+contactName+"    联系人电话："+phoneNumber+"\n");
            }
        }
        cursor.close();
    }
}
